<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - TCP/UDP测试服务管理</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
    <style>
        body {
            background-color: #f5f5f5;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        .login-container {
            width: 100%;
            max-width: 400px;
            padding: 15px;
            margin: auto;
        }
        .form-signin {
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }
        .form-signin .form-floating:focus-within {
            z-index: 2;
        }
        .form-signin input[type="text"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
        }
        .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
        }
        .alert {
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="form-signin">
            <div class="text-center mb-4">
                <h1 class="h3 mb-3 font-weight-normal">TCP/UDP测试服务管理</h1>
                <p>请登录以继续</p>
            </div>
            
            <div class="alert alert-danger" id="errorAlert" role="alert">
                用户名或密码错误
            </div>
            
            <div class="form-floating mb-3">
                <input type="text" class="form-control" id="username" placeholder="用户名">
                <label for="username">用户名</label>
            </div>
            
            <div class="form-floating mb-3">
                <input type="password" class="form-control" id="password" placeholder="密码">
                <label for="password">密码</label>
            </div>
            
            <button class="w-100 btn btn-lg btn-primary" id="loginBtn">登录</button>
            
            <!-- <p class="mt-3 text-muted text-center">默认用户名: thd, 密码: thd123_tcptest</p> -->
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('loginBtn').addEventListener('click', function() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            if (!username || !password) {
                document.getElementById('errorAlert').style.display = 'block';
                document.getElementById('errorAlert').textContent = '请输入用户名和密码';
                return;
            }
            
            fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.location.href = '/index.html';
                } else {
                    document.getElementById('errorAlert').style.display = 'block';
                    document.getElementById('errorAlert').textContent = data.message || '用户名或密码错误';
                }
            })
            .catch(error => {
                document.getElementById('errorAlert').style.display = 'block';
                document.getElementById('errorAlert').textContent = '登录请求失败，请稍后再试';
                console.error('Error:', error);
            });
        });
        
        // 支持按回车键登录
        document.getElementById('password').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                document.getElementById('loginBtn').click();
            }
        });
    </script>
</body>
</html>